﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Interfaces;
using DataAccess;
using DCMS_Framework;
using BusinessObjects;
using DCMSExceptions;
using System.Data;

namespace BusinessLogic {

  /// <summary>
  /// Business Layer Logic for Zipcodes
  /// </summary>
  public class ZipcodeBll : IZipcodeLogic {

    ZipcodeDal dal = new ZipcodeDal( );

    #region - Public methods -

    /// <summary>
    /// Gets the zipcode.
    /// </summary>
    /// <param name="id">The id.</param>
    /// <returns></returns>
    public CityZipcode GetZipcode( int id ) {
      CityZipcode zipcode;
      try {
        zipcode = dal.GetZipcode( id );
      } catch ( DataException ex ) {
        throw new BusinessException( "Fout bij ophalen postcode.", ex );
      }
      return zipcode;
    }

    /// <summary>
    /// Gets all zipcodes.
    /// </summary>
    /// <returns></returns>
    public SortableBindingList<CityZipcode> GetAllZipcodes( ) {
      SortableBindingList<CityZipcode> list;
      try {
        list = dal.GetAllZipcodes( );
      } catch ( DataException ex ) {
        throw new BusinessException( "Fout bij ophalen van postcodes.", ex );
      }
      return list;
    }

    /// <summary>
    /// Gets the zipcodes by country.
    /// </summary>
    /// <param name="countryId">The country id.</param>
    /// <returns></returns>
    public SortableBindingList<CityZipcode> GetZipcodesByCountry( int countryId ) {
      SortableBindingList<CityZipcode> list;
      try {
        list = dal.GetZipcodesByCountry( countryId );
      } catch ( DataException ex ) {
        throw new BusinessException( "Fout bij ophalen postcode.", ex );
      }
      return list;
    }

    /// <summary>
    /// Deletes the zipcode.
    /// </summary>
    /// <param name="zipcode">The zipcode.</param>
    public void DeleteZipcode( BusinessObjects.CityZipcode zipcode ) {
      try {
        dal.DeleteZipcode( zipcode );
      } catch ( DataException ex ) {
        throw new BusinessException( "Fout bij verwijderen postcode.", ex );
      }
    }

    /// <summary>
    /// Saves the zipcode.
    /// </summary>
    /// <param name="zipcode">The zipcode.</param>
    public void SaveZipcode( BusinessObjects.CityZipcode zipcode ) {
      if ( Validate( zipcode ) ) {
        try {
          dal.SaveZipcode( zipcode );
        } catch ( DataException ex ) {
          throw new BusinessException( ex.Message, ex );
        }
      }

    }

    /// <summary>
    /// Gets the countries.
    /// </summary>
    /// <returns></returns>
    public List<Country> GetCountries( ) {
      List<Country> countries = null;
      try {
        countries = dal.GetCountries( );
      } catch ( DataException ex ) {
        throw new BusinessException( ex.Message, ex );
      }
      return countries;
    }

    #endregion

    private bool Validate( CityZipcode zipcode ) {

      if ( string.IsNullOrEmpty( zipcode.Zipcode ) ) {
        zipcode.AddBrokenRule( "ZipcodeRule", "Postcode niet ingevuld.", "Zipcode" );
      }
      if ( string.IsNullOrEmpty( zipcode.City ) ) {
        zipcode.AddBrokenRule( "CityRule", "Gemeente niet ingevuld.", "City" );
      }

      return zipcode.IsValid;
    }

  }
}
